package com.ypinkj.app.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ypinkj.app.annotation.UserLoginToken;
import com.ypinkj.app.entity.CCompanyPostjobVO;
import com.ypinkj.app.entity.Dict;
import com.ypinkj.app.mapper.CCompanyPostjobMapper;
import com.ypinkj.app.mapper.DictDao;
import com.ypinkj.app.service.CCompanyPostjobService;
import com.ypinkj.app.utils.resultutils.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ypinkj.app.annotation.UserLoginToken;
import com.ypinkj.app.entity.CCompany;
import com.ypinkj.app.entity.CCompanyPostjob;
import com.ypinkj.app.entity.Dict;
import com.ypinkj.app.entity.PositionVo;
import com.ypinkj.app.mapper.CCompanyMapper;
import com.ypinkj.app.mapper.CCompanyPostjobMapper;
import com.ypinkj.app.mapper.DictDao;
import com.ypinkj.app.utils.resultutils.Result;
import com.ypinkj.app.utils.resultutils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * 前端控制器  发布职位
 * </p>
 *
 * @author LF
 * @since 2020-09-29
 */
@Api(tags = "职位管理")
@RestController
@RequestMapping("/c-company-postjob")
public class CCompanyPostjobController {

    @Autowired
    CCompanyPostjobMapper companyPostjobMapper;

    @Autowired
    CCompanyMapper companyMapper;

    @Autowired
    private CCompanyPostjobService cCompanyPostjobService;

    @Autowired
    private CCompanyPostjobMapper cCompanyPostjobMapper;

    @Autowired
    private DictDao dictDao;

    /**
     * 获取热门职位
     *
     * @return
     */

    //@UserLoginToken
    @RequestMapping("getPopularPositions")
    public ResultPage getPopularPositions(Integer page, Integer limit) {

        //   a();

        List<CCompanyPostjobVO> popularPositions = cCompanyPostjobService.findPopularPositions(page - 1, limit);

        Long count = Long.valueOf(cCompanyPostjobMapper.getCount());


        for (int i = 0; i < popularPositions.size(); i++) {
            //获取福利 标签 编号
            String welfarelabelids = popularPositions.get(i).getWelfarelabelids();
            //拆分获取 单个
            String[] split = welfarelabelids.split(",");
            List<String> wids = Arrays.asList(split);


            List<Dict> dicts = dictDao.selectBatchIds(wids);

            popularPositions.get(i).setWelfarelabelList(dicts);

        }


        return  ResultUtil.successCount(popularPositions, count);


    }


    /**
     * 获取职位详细
     */

    //@UserLoginToken
    @RequestMapping("getJobDetails")
    public Result getJobDetails(@RequestParam("positionId") String positionId) {


        CCompanyPostjobVO jobDetails = cCompanyPostjobMapper.getJobDetails(positionId);
        if(jobDetails==null){
            ResultUtil.error("该职位不存在");
        }



        String welfarelabelids = jobDetails.getWelfarelabelids();

        String[] split = welfarelabelids.split(",");
        List<String> wids = Arrays.asList(split);

        List<Dict> dicts = dictDao.selectBatchIds(wids);

        jobDetails.setWelfarelabelList(dicts);


        return ResultUtil.success(jobDetails);
    }


    /**
     * @param companyarea
     * @param position
     * @return
     */

    //@UserLoginToken
    @RequestMapping("recommendAPosition")
    public Result recommendAPosition(@RequestParam("companyarea") String companyarea, @RequestParam("position") String position, Integer page, Integer limit) {


        System.out.println(companyarea.toString());

        List<String> companyareaList = Arrays.asList(companyarea.split(","));
        List<String> positionList = Arrays.asList(position.split(","));


        List<CCompanyPostjobVO> cCompanyPostjobVOS = cCompanyPostjobMapper.recommendAPosition(companyareaList, positionList, page - 1, limit);


        return ResultUtil.success(cCompanyPostjobVOS);
    }


    public void a() {

        List<String> companyarea = new ArrayList<>();
        companyarea.add("1");
        companyarea.add("110101");

        List<String> position = new ArrayList<>();
        position.add("13");
        position.add("1");

        //    List<CCompanyPostjobVO> cCompanyPostjobVOS = cCompanyPostjobMapper.recommendAPosition(companyarea, position);

    }


    //期望薪资 expectSalary
    //工作年限 work_years
    //学历 educate
    //福利标签 corporateWelfare
    @ApiOperation(value = "按标签类型选择数据")
    @GetMapping("/getListByType")
    public Result getListByType(String type) {
        LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dict::getType, type);
        List<Dict> dicts = dictDao.selectList(queryWrapper);
        return ResultUtil.success(dicts);
    }

    //页面直接获取登录成功后返回实体类的公司名称，暂时不需要请求接口
    @ApiOperation(value = "获取公司名称")
    @GetMapping("/getCompany")
    public Result getCompany(String token) {
        LambdaQueryWrapper<CCompany> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CCompany::getToken, token);
        List<CCompany> list = companyMapper.selectList(queryWrapper);
        return ResultUtil.success(list.get(0).getCompanyname());
    }

    @ApiOperation(value = "发布职位保存")
    @PutMapping
    public Result Save(@RequestBody CCompanyPostjob companyPostjob) {
        //int i = companyPostjobMapper.insert(companyPostjob);
        return companyPostjobMapper.insert(companyPostjob) > 0 ? ResultUtil.success() : ResultUtil.errorMsg();
    }

    @ApiOperation(value = "职位管理")
    @GetMapping("/getList")
    public Result getList(String token) {
        List<PositionVo> positionList = companyPostjobMapper.getPositionList(token);
        return positionList.size() > 0 ? ResultUtil.success(positionList) : ResultUtil.error("暂无数据");
    }

}
